package util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.*;

public class StringHelper {

	public StringHelper() {
	}

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static int countOccurrencesOf(String s, String sub) {
		if (s == null || sub == null || "".equals(sub))
			return 0;
		int count = 0;
		int pos = 0;
		for (int idx = 0; (idx = s.indexOf(sub, pos)) != -1;) {
			count++;
			pos = idx + sub.length();
		}

		return count;
	}

	public static String replace(String inString, String oldPattern,
			String newPattern) {
		if (inString == null)
			return null;
		if (oldPattern == null || newPattern == null)
			return inString;
		else
			return inString.replaceAll(oldPattern, newPattern);
	}

	public static String delete(String inString, String pattern) {
		return replace(inString, pattern, "");
	}

	public static String deleteAny(String inString, String chars) {
		if (inString == null || chars == null)
			return inString;
		StringBuffer out = new StringBuffer();
		for (int i = 0; i < inString.length(); i++) {
			char c = inString.charAt(i);
			if (chars.indexOf(c) == -1)
				out.append(c);
		}

		return out.toString();
	}

	public static String[] delimitedListToStringArray(String s, String delimiter) {
		if (s == null)
			return new String[0];
		if (delimiter == null)
			return (new String[] { s });
		List l = new LinkedList();
		int pos = 0;
		for (int delpos = 0; (delpos = s.indexOf(delimiter, pos)) != -1;) {
			l.add(s.substring(pos, delpos));
			pos = delpos + delimiter.length();
		}

		if (pos <= s.length())
			l.add(s.substring(pos));
		return (String[]) l.toArray(new String[l.size()]);
	}

	public static String[] commaDelimitedListToStringArray(String s) {
		return delimitedListToStringArray(s, ",");
	}

	public static Set commaDelimitedListToSet(String s) {
		Set set = new TreeSet();
		String tokens[] = commaDelimitedListToStringArray(s);
		for (int i = 0; i < tokens.length; i++)
			set.add(tokens[i]);

		return set;
	}

	public static String collectionToDelimitedString(Collection c, String delim) {
		if (c == null)
			return "null";
		else
			return iteratorToDelimitedString(c.iterator(), delim);
	}

	public static String iteratorToDelimitedString(Iterator itr, String delim) {
		if (itr == null)
			return "null";
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for (; itr.hasNext(); sb.append(itr.next()))
			if (i++ > 0)
				sb.append(delim);

		return sb.toString();
	}

	public static String getFileName(String path) {
		return getFileName(path, File.pathSeparator);
	}

	public static String getFileName(String path, String pathSeparator) {
		if (path == null)
			return null;
		int position = path.lastIndexOf(pathSeparator);
		if (position >= 0)
			return path.substring(position + 1);
		else
			return path;
	}

	public static Long[] stringArrayToLongArray(String strs[]) {
		if (strs == null)
			return null;
		if (strs.length == 0)
			return new Long[0];
		Long result[] = new Long[strs.length];
		for (int i = 0; i < strs.length; i++)
			result[i] = new Long(strs[i]);

		return result;
	}

	public static String[] objectArrayToStringArray(Object objs[]) {
		if (objs == null)
			return null;
		String result[] = new String[objs.length];
		for (int i = 0; i < result.length; i++)
			result[i] = objs[i].toString();

		return result;
	}

	public static String parseVariable(String toParse, Properties variables) {
		if (isEmpty(toParse))
			return toParse;
		StringBuffer buf = new StringBuffer();
		int index = 0;
		int end;
		for (int last = -1; last <= toParse.length() - 4; last = end) {
			index = toParse.indexOf("${", last);
			if (index < 0) {
				buf.append(toParse.substring(last + 1));
				break;
			}
			buf.append(toParse.substring(last + 1, index));
			end = toParse.indexOf("}", index);
			if (end < 0) {
				buf.append(toParse.substring(last + 1));
				break;
			}
			String key = toParse.substring(index + 2, end);
			if (!"".equals(key))
				buf.append(variables.get(key));
		}

		return buf.toString();
	}
	public static String toUTF8(String s){
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= 255) {
				sb.append(c);
			} else {
				byte[] b;
				try {
					b = Character.toString(c).getBytes("utf-8");
				} catch (Exception ex) {
					System.out.println(ex);
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}
	public static String toGBK(String str){
		try {
			return new String(str.getBytes("GBK"),"ISO8859_1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return str;
		}
	}
	public static String toString(Object object) {
		return object != null ? object.toString() : "";
	}
}
